<template>
  <div>
    <el-card style="width: 50%">
      <el-form ref="formRef" :rules="rules"  size="medium" :label-position="labelPosition" :model="user" label-width="80px"
               style="padding-right: 20px">
        <el-form-item label="账号" prop="no" style="width: 60%">
          <el-input v-model="user1.no" placeholder="账号" readonly></el-input>
        </el-form-item>
        <el-form-item label="原始密码" prop="password"style="width: 60%" >
          <el-input show-password v-model="user.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword" style="width: 60%">
          <el-input show-password v-model="user.newPassword" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword" style="width: 60%">
          <el-input show-password v-model="user.confirmPassword" placeholder="请确认新密码"></el-input>
        </el-form-item>
        <div style="text-align: center;margin-bottom: 20px">
          <el-button type="primary" @click="update">确认修改</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {updatePassword} from "@/api/house";

export default {
  name: "PasswordManage",
  data() {
    //校验确认密码
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请输入确认密码'));
      }
      else if (confirmPass!== this.user.newPassword) {
        callback(new Error('两次输入的密码不一致'))
      }
      else {
        callback();
      }
    }
    return {
      labelPosition: 'left',
      user:{
        password: '',
        newPassword:'',
        confirmPassword:''
      },
      user1: JSON.parse(sessionStorage.getItem("CurUser")),
      rules: {
        password: [
          {required: true, message: '请输入原始密码', trigger: 'blur'},
        ],
        newPassword: [
          {required: true, message: '请输入新密码', trigger: 'blur'},
        ],
        confirmPassword: [
          {validator: validatePassword,required: true,  trigger: 'blur'},
        ]
      }
    }
  },
  methods:{
    update() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.user1.password=this.user.newPassword
          //验证通过
          updatePassword(this.user1).then(res => res.data).then(res => {
            console.log(res)
            if (res.code == 200) {
              //跳转到主页
              this.$router.push('/')
              sessionStorage.clear()
              localStorage.clear()
              this.$message.success("修改密码成功，请重新登录")
            }
            else {
              this.$message.error(res.msg)
              return false;
            }
          })
        }
        else {
          console.log('校验失败');
          return false;
        }
      })



        //   this.$axios.post(this.$httpUrl + '/user/updatePassword', this.user1).then(res => res.data).then(res => {
        //     console.log(res)
        //     if (res.code == 200) {
        //       //跳转到主页
        //       this.$router.push('/')
        //       sessionStorage.clear()
        //       localStorage.clear()
        //       this.$message.success("修改密码成功，请重新登录")
        //     }
        //     else {
        //       this.$message.error(res.msg)
        //       return false;
        //     }
        //   })
        // }
      //   else {
      //     console.log('校验失败');
      //     return false;
      //   }
      // })
    }
  }
}
</script>

<style scoped>

</style>